package arithmetic_40;

/**
 *
 * @author wxh
 */
public class Solution_10 {

	public int mySqrt(int x) {
		if (x == 0 || x == 1) {
			return x;
		}

		int l = 1;
		int r = x;
		int result = 0;
		while (l <= r) {
			int m = (l + r) / 2;
			int mm = m * m;
			if (mm == x) {
				return m;
			} else if (mm > x) {
				r = m - 1;
			} else {
				l = m + 1;
				result = m;
			}
		}
		return result;
	}

	public static void main(String[] args) {
		Solution_10 solution_10 = new Solution_10();
		System.out.println(solution_10.mySqrt(50));
	}

}